package com.bailong.administrator.ausedcar.ui.homepagefragment;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bailong.administrator.ausedcar.R;
import com.bailong.administrator.ausedcar.bean.HomeHotBean;
import com.bailong.administrator.ausedcar.bean.HomeLikeBean;
import com.bailong.administrator.ausedcar.bean.HomeShowBean;
import com.bailong.administrator.ausedcar.ui.brand.BrandActivity;
import com.bailong.administrator.ausedcar.ui.detail.DetailActivity;
import com.bailong.administrator.ausedcar.ui.payhalf.PayHalfActivity;
import com.bailong.administrator.ausedcar.ui.search.SearchActivity;
import com.bailong.administrator.ausedcar.widget.MyGridView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
//首页碎片
public class HomePageFragment extends Fragment implements HomeContract.View {
    @Bind(R.id.fragment_home_page_lv)
    ListView fragmentHomePageLv;
    @Bind(R.id.fragment_home_page_mgv)
    MyGridView fragmentHomePageMgv;
    @Bind(R.id.fragment_home_page_tbl)
    TabLayout fragmentHomePageTbl;
    @Bind(R.id.fragment_home_page_show_tv_name)
    TextView fragmentHomePageShowTvName;
    @Bind(R.id.fragment_home_page_show_tv_price)
    TextView fragmentHomePageShowTvPrice;
    @Bind(R.id.fragment_home_page_show_iv)
    ImageView fragmentHomePageShowIv;
    @Bind(R.id.fragment_home_page_show_tv_name_up)
    TextView fragmentHomePageShowTvNameUp;
    @Bind(R.id.fragment_home_page_show_tv_price_up)
    TextView fragmentHomePageShowTvPriceUp;
    @Bind(R.id.fragment_home_page_show_iv_up)
    ImageView fragmentHomePageShowIvUp;
    @Bind(R.id.fragment_home_page_show_tv_name_down)
    TextView fragmentHomePageShowTvNameDown;
    @Bind(R.id.fragment_home_page_show_tv_price_down)
    TextView fragmentHomePageShowTvPriceDown;
    @Bind(R.id.fragment_home_page_show_iv_down)
    ImageView fragmentHomePageShowIvDown;
    @Bind(R.id.fragment_home_page_lnl)
    LinearLayout fragmentHomePageLnl;
    @Bind(R.id.fragment_home_page_show_tv_payhalf)
    TextView fragmentHomePageShowTvPayhalf;
    @Bind(R.id.fragment_home_page_iv)
    ImageView fragmentHomePageIv;
    @Bind(R.id.fragment_home_page_show_down)
    RelativeLayout fragmentHomePageShowDown;
    @Bind(R.id.fragment_home_page_show_up)
    LinearLayout fragmentHomePageShowUp;
    @Bind(R.id.fragment_home_page_show_left)
    LinearLayout fragmentHomePageShowLeft;

    private View view;
    private Activity activity;
    private HomeLvAdapter homeLvAdapter;
    private HomeHotAdapter homeHotAdapter;
    private List<HomeLikeBean.DataBean.ListBean> likeList = new ArrayList<>();
    private List<HomeHotBean.DataBean.ListBean> hotList = new ArrayList<>();
    private List<HomeShowBean.DataBean.RecommendBean> showList= new ArrayList<>();
    private HomeInter homeInter;
    private HomePageFragment homePageFragment = this;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        homeLvAdapter = new HomeLvAdapter(likeList, activity);
        homeHotAdapter = new HomeHotAdapter(hotList, activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home_page, container, false);
        ButterKnife.bind(this, view);
        initView();
        setAdapter();
        return view;
    }

    private void initView() {
        HomePresenter homePresenter = new HomePresenter(this);
        homePresenter.loadHomeLikeBean();
        homePresenter.loadHomeHotBean();
        homePresenter.loadHomeShowBean();
        fragmentHomePageTbl.setTabTextColors(Color.BLACK, Color.RED);
        fragmentHomePageTbl.addTab(fragmentHomePageTbl.newTab().setText("猜你喜欢"), 0);
        fragmentHomePageTbl.addTab(fragmentHomePageTbl.newTab().setText("浏览历史"), 1);
    }

    private void setAdapter() {
        fragmentHomePageLv.setAdapter(homeLvAdapter);
        fragmentHomePageMgv.setAdapter(homeHotAdapter);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEvent();
    }

    private void setEvent() {
        fragmentHomePageShowLeft.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (showList.size()>0){
                    Intent intent = new Intent(activity, SearchActivity.class);
                    String serieid = showList.get(0).getSearch().getSerieid();
                    intent.putExtra("serieid",serieid);
                    startActivity(intent);
                }

                return true;
            }
        });
        fragmentHomePageShowDown.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (showList.size()>0){
                    Intent intent = new Intent(activity, SearchActivity.class);
                    String serieid = showList.get(1).getSearch().getSerieid();
                    intent.putExtra("serieid",serieid);
                    startActivity(intent);
                }
                return true;
            }
        });
        fragmentHomePageShowUp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (showList.size()>0){
                    Intent intent = new Intent(activity, SearchActivity.class);
                    String serieid = showList.get(2).getSearch().getSerieid();
                    intent.putExtra("serieid",serieid);
                    startActivity(intent);
                }
                return true;
            }
        });

        fragmentHomePageLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(activity, DetailActivity.class);
                intent.putExtra("carid", likeList.get(position).getCarid());
                startActivity(intent);
                //准备跳转
            }
        });
        fragmentHomePageMgv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(activity, BrandActivity.class);
                intent.putExtra("brandid", hotList.get(position).getBrandid());
                intent.putExtra("position", position);

                startActivity(intent);
            }
        });
        fragmentHomePageIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, PayHalfActivity.class);
                startActivity(intent);
            }
        });
        fragmentHomePageShowTvPayhalf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(activity, PayHalfActivity.class);
                startActivity(intent);
            }
        });
        fragmentHomePageLnl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (homeInter != null)
                    homeInter.send(homePageFragment);
                return true;
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResponse(HomeLikeBean bean) {
        homeLvAdapter.reloadData(bean.getData().getList());
    }

    @Override
    public void onResponse(HomeHotBean bean) {
        homeHotAdapter.reloadData(bean.getData().getList());
    }

    @Override
    public void onResponse(final HomeShowBean bean) {
        fragmentHomePageShowTvName.setText(bean.getData().getRecommend().get(0).getName());
        fragmentHomePageShowTvPrice.setText(bean.getData().getRecommend().get(0).getPrice());
        Glide.with(activity).load(bean.getData().getRecommend().get(0).getImg()).into(fragmentHomePageShowIv);
        fragmentHomePageShowTvNameUp.setText(bean.getData().getRecommend().get(1).getName());
        fragmentHomePageShowTvPriceUp.setText(bean.getData().getRecommend().get(1).getPrice());
        Glide.with(activity).load(bean.getData().getRecommend().get(1).getImg()).into(fragmentHomePageShowIvUp);
        fragmentHomePageShowTvNameDown.setText(bean.getData().getRecommend().get(2).getName());
        fragmentHomePageShowTvPriceDown.setText(bean.getData().getRecommend().get(2).getPrice());
        Glide.with(activity).load(bean.getData().getRecommend().get(2).getImg()).into(fragmentHomePageShowIvDown);

        showList=bean.getData().getRecommend();

    }

    public interface HomeInter {
        void send(HomePageFragment homePageFragment);
    }

    public void setLinearLayoutListener(HomeInter homeInter) {
        this.homeInter = homeInter;
    }

}
